<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0px;
            padding: 0px;
            color: rgb(red, green, blue);
        }

        html,
        body {
            height: 100%;
            width: 100%;
        }

        body {
            background-color: black;
        }

        .canvas {
            font-size: 0px;
            margin: 0px auto;
            border: 1px solid black;
            position: relative;
        }

        .buttons {
            text-align: center;
        }

        .buttons button {
            margin: 20px;
        }

        @keyframes change {
            100% {}

        }
    </style>
</head>

<body>
    <div class="canvas"></div>
    <p class="buttons">
        <button onclick="add()">添加</button>
        <button onclick="stop()">暂停添加</button>
        <button onclick="change()">变色</button>
        <button onclick="stopC()">暂停变色</button>
        <input type="text" placeholder="请输入球数" id="num">
    </p>
    <script>
        var canvas = document.querySelector(".canvas")
        var time
        var timeC
        var blocks = []
        var Top = document.body.clientHeight / 2 - 110
        var Left = document.body.clientWidth / 2 - 170
        function add() {
            var num = parseInt(document.getElementById("num").value) || 10
            if (!time) {
                time = setInterval(function () {
                    var block = document.createElement("div")
                    block.style.width = "10px"
                    block.style.height = "10px"
                    block.style.backgroundColor = "yellow"
                    block.style.border = "1px solid black"
                    block.style.boxSizing = "border-box"
                    block.style.position = "absolute"
                    block.style.top = Math.round(Top + Math.random() * 220) + "px"
                    block.style.left = Math.round(Left + Math.random() * 340) + "px"
                    block.style.borderRadius = "50%"
                    blocks.push(block)
                    if (blocks.length > num) {
                        canvas.removeChild(blocks[0])
                        blocks.shift()
                    }
                    canvas.append(block)
                }, 100)
            }



        }
        function change() {
            timeC = setInterval(function () {
                for (i in blocks) {
                    var x = Math.round(Math.random() * 40)
                    var y = Math.round(Math.random() * 40)
                    //每个球移动位置随机四舍五入
                    var r = Math.round(Math.random() * 255)
                    var g = Math.round(Math.random() * 255)
                    var b = Math.round(Math.random() * 255)
                    // 随机颜色, 四舍五入
                    var top = parseInt(blocks[i].style.top)
                    var left = parseInt(blocks[i].style.left)
                    //去除px
                    if (top <= Top + 110 && left <= Left + 170) {
                        blocks[i].style.top = (top - y) + "px"
                        blocks[i].style.left = (left - x) + "px"
                    }
                    else if (top <= Top + 110 && left >= Left + 170) {
                        blocks[i].style.top = (top - y) + "px"
                        blocks[i].style.left = (left + x) + "px"
                    }
                    else if (top >= Top + 110 && left >= Left + 170) {
                        blocks[i].style.top = (top + y) + "px"
                        blocks[i].style.left = (left + x) + "px"
                    }
                    else if (top >= Top + 110 && left <= Left + 170) {
                        blocks[i].style.top = (top + y) + "px"
                        blocks[i].style.left = (left - x) + "px"
                    }
                    blocks[i].style.backgroundColor = "rgb(" + r + "," + g + "," + b + ")"
                    blocks[i].style.width = (parseInt(blocks[i].style.width) + 1) + "px"
                    blocks[i].style.height = (parseInt(blocks[i].style.height) + 1) + "px"
                }
            }, 100)

        }
        function stop() {
            clearInterval(time)
            time = ""
        }
        function stopC() {
            clearInterval(timeC)
            for (i in blocks) {
                blocks[i].style.backgroundColor = "yellow"
            }
        }
    </script>
</body>


</html>